package com.tzsp.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.xml.internal.resolver.CatalogManager;
import com.tzsp.reggie.commons.contans.R;
import com.tzsp.reggie.commons.utils.BusinessException;
import com.tzsp.reggie.domain.*;
import com.tzsp.reggie.mapper.CategoryMapper;
import com.tzsp.reggie.mapper.EmployeeMapper;
import com.tzsp.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除菜品分类
     *  条件
     * @param ids
     */
    @Override
    public void deleteDishCategory(Long ids) {

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,ids);
        int dishCount = dishService.count(dishLambdaQueryWrapper);
        //查询菜品分类是否与菜品关联，如果关联则抛异常
        if (dishCount>0){
            //抛异常
            throw  new BusinessException("该分类已与菜品关联！请先删除菜品");
        }
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
       setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,ids);
        int setmealCount = setmealService.count(setmealLambdaQueryWrapper);
        //查询菜品分类是否与套餐关联，如果关联则抛异常
        if (setmealCount>0){
            //抛异常
            throw  new BusinessException("该分类已与套餐关联！请先删除菜品");
        }
        //都未关联则正常删除
        super.removeById(ids);
    }


}

